#include <sys/sysinfo.h>
#include <unistd.h>
#include <stdio.h>

#define N 10
#define MAXITER 10

double grid[N+1][N+1];
double newg[N+1][N+1];

double maxdiff=0.0;

double _max_(double x,double y)
{
  return x>y ? x : y;
}

double _abs_(double x)
{
  return x>0 ? x : (-1)*x;
}

int main(int argc,char* argv[])
{
  int iter,i,j;
  
  printf("Hello world from Jacobi iteration!");
  
  // init omitted
  
  for(iter=1; iter<=MAXITER; iter+=2){
    for(i=1;i<=N;i++)for(j=1;j<=N;j++)
      newg[i][j]=0.25*(grid[i-1][j]+grid[i+1][j]+grid[i][j-1]+grid[i][j+1]);

    for(i=1;i<=N;i++)for(j=1;j<=N;j++)
      grid[i][j]=0.25*(newg[i-1][j]+newg[i+1][j]+newg[i][j-1]+newg[i][j+1]);
  }
  
  for(i=1;i<=N;i++)for(j=1;j<=N;j++)
    maxdiff=_max_(maxdiff,_abs_(grid[i][j]-newg[i][j]));
  
  printf(" precision=%lf",maxdiff);
  
  printf(" sysconf_configured=%ld",sysconf(_SC_NPROCESSORS_CONF));
  printf(" sysconf_online=%ld",sysconf(_SC_NPROCESSORS_ONLN));
  printf(" get_nprocs_conf=%d",get_nprocs_conf());
  printf(" get_nprocs=%d",get_nprocs());
  
  return 0;
}

